﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Pixysoft.Framework.Logs.Core
{
    class RecordPackageController
    {
        private static volatile RecordPackageController instance;

        private static object syncRoot = new Object();

        public static RecordPackageController Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new RecordPackageController();
                    }
                }

                return instance;

            }
        }

        /// <summary>
        /// millicsecago 表示在此时间之前的打包，之后的不打包
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <param name="postfix"></param>
        /// <param name="milliSecAgo"></param>
        /// <returns></returns>
        public string Package(string path, string name, string postfix, int milliSecAgo)
        {
            List<FileInfo> list = new List<FileInfo>();

            DateTime dateto = DateTime.MinValue;

            foreach (string file in GetFiles(path, name, postfix, milliSecAgo, out dateto))
            {
                list.Add(new FileInfo(file));
            }

            if (list.Count == 0)
                return null;

            string descfilename = path + name + "." + dateto.ToString(StringHelper.default_log_pack_dateformat) +
    StringHelper.default_compress_postfix;

            Pixysoft.IO.GZipResult result = Pixysoft.IO.BatchCompressor.Compress(
                list.ToArray(), descfilename);

            foreach (FileInfo info in list)
            {
                info.Delete();
            }

            return descfilename;
        }

        private List<string> GetFiles(string path, string name, string postfix, int milliSecAgo, out DateTime dateto)
        {
            dateto = DateTime.MinValue;

            List<string> list = new List<string>();

            DateTime time = Pixysoft.Tools.GlobalTimer.Instance.GetGlobalTime().AddMilliseconds(-1 * milliSecAgo);

            foreach (string file in Directory.GetFiles(path))
            {
                string filename = StringFilter.GetFileName(file);

                if (!filename.StartsWith(name, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!filename.EndsWith(postfix, StringComparison.OrdinalIgnoreCase))
                    continue;

                //2009-12-22 如果millisecago < 0 表示全打包

                if (filename.Equals(name + postfix, StringComparison.OrdinalIgnoreCase) && milliSecAgo >= 0)
                    continue;


                DateTime lasttime = DateTime.MinValue;

                if (!StringHelper.TryParseFileSequence(filename, out lasttime))
                {
                    lasttime = time;
                }

                if (lasttime > time)
                    continue;

                if (dateto < lasttime)
                    dateto = lasttime;

                if (!list.Contains(file))
                    list.Add(file);
            }

            return list;
        }



    }
}
